/**
 * @author:cavion(曹仁道)
 * @email:caorendao187@163.com
 * 2016年9月10日 
 */
package org.rk.core.jdbc.dao.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import org.rk.core.common.anno.FieldMeta;
import org.rk.core.common.anno.TableMeta;
import org.rk.core.common.util.RkObjectUtil;
import org.rk.core.common.util.RkStrUtil;
import org.springframework.util.CollectionUtils;

/**
 * @author:cavion
 * @描述：
 * 2016年9月10日 上午9:47:36
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class SQLUtils {
	private static Logger log = Logger.getLogger(SQLUtils.class);
	
	private static Map cacheMap = new HashMap();
    private static Map insertSqlCache = new HashMap();
    private static Map updateSqlCache = new HashMap(); 
    private static Map deleteSqlCache = new HashMap();
    private static Map selectSqlCache = new HashMap();
    
    /**
     * 
     * 2016年9月10日 上午10:27:19
     * @author:cavion
     * @描述：根据pojo类的class来构建select * from 的SQL语句
     * @param pojoClass
     * @return
     */
	public static String buildSelectSql(Class pojoClass){
    	List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
        String sql = buildSelectSql(pojoClass, fieldInfoList);
        if(log.isDebugEnabled()){
            log.debug("select sql is:"+sql);
        }
        return sql;
    }
	/**
	 * 
	 * 2016年9月10日 上午10:27:58
	 * @author:cavion
	 * @描述：根据pojo类的class来构建insert的SQL语句
	 * @param pojoClass
	 * @return
	 */
	public static String buildInsertSql(Class pojoClass){
        List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
        String sql = buildInsertSql(pojoClass, fieldInfoList);
        if(log.isDebugEnabled()){
            log.debug("insert sql is:"+sql);
        }
        return sql;
    }
	/**
	 * 
	 * 2016年9月10日 上午10:32:53
	 * @author:cavion
	 * @描述：根据pojo类的class构建根据pk来update的SQL语句
	 * @param pojoClass
	 * @return
	 */
	public static String buildUpdateSqlByPk(Object objBean) {
		List<FieldInfo> fieldInfoList = loadPojoSqlInfo(objBean.getClass());
		String sql = buildUpdateSqlByPK(objBean, fieldInfoList);
		if (log.isDebugEnabled()) {
			log.debug("update sql is:" + sql);
		}
		return sql;
	}
	/**
	 * 
	 * 2016年9月10日 上午10:32:53
	 * @author:cavion
	 * @描述：根据pojo类的class构建根据pk来update的SQL语句
	 * @param pojoClass
	 * @return
	 */
	public static String buildUpdateSqlByUnique(Object objBean) {
		List<FieldInfo> fieldInfoList = loadPojoSqlInfo(objBean.getClass());
		String sql = buildUpdateSqlByUnique(objBean, fieldInfoList);
		if (log.isDebugEnabled()) {
			log.debug("update sql is:" + sql);
		}
		return sql;
	}
	/**
	 * 
	 * 2016年9月10日 上午10:37:06
	 * @author:cavion
	 * @描述：根据pojo类的Class和更新的条件字段来生成upate的SQL语句
	 * @param pojoClass
	 * @param columns
	 * @return
	 * @throws Exception
	 */
	public static String buildUpdateSqlByColumns(Class pojoClass, String[] columns) throws Exception {
		if (null != columns && columns.length > 0) {
			List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
			String sql = buildUpdateSqlByColumns(pojoClass, fieldInfoList, columns);
			if (log.isDebugEnabled()) {
				log.debug("update sql is:" + sql);
			}
			return sql;
		} else {
			if (log.isDebugEnabled()) {
				log.debug("生成update sql error! 参数columns必须有值");
			}
			throw new Exception("参数columns必须有值！");
		}
	}
	/**
	 * 
	 * 2016年9月10日 上午10:39:41
	 * @author:cavion
	 * @描述：根据pojo类的Class生成根据pk来delete的SQL语句
	 * @param pojoClass
	 * @return
	 */
	public static String buildDeleteSqlByPk(Class pojoClass) {
		List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
		String sql = buildDeleteSqlByPK(pojoClass, fieldInfoList);
		if (log.isDebugEnabled()) {
			log.debug("delete sql is:" + sql);
		}
		return sql;
	}
	/**
	 * @author：cavion
	 * @方法描述：根据pojo类的Class生成根据unique来delete的SQL语句
	 * @param pojoClass
	 * @return
	 */
	public static String buildDeleteSqlByUnique(Class pojoClass) {
		List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
		String sql = buildDeleteSqlByUnique(pojoClass, fieldInfoList);
		if (log.isDebugEnabled()) {
			log.debug("delete sql is:" + sql);
		}
		return sql;
	}
	/**
	 * 
	 * 2016年9月10日 上午10:44:02
	 * @author:cavion
	 * @描述：根据pojo类的Class和更新的条件字段来生成delete的SQL语句
	 * @param pojoClass
	 * @param columns
	 * @return
	 * @throws Exception
	 */
	public static String buildDeleteSqlByColumns(Class pojoClass,String[] columns) throws Exception{
        if(null!=columns && columns.length>0){
            List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
            String sql = buildDeleteSqlByColumns(pojoClass, fieldInfoList, columns);
            if(log.isDebugEnabled()){
                log.debug("delete sql is:"+sql);
            }
            return sql;
        }else{
            if(log.isDebugEnabled()){
                log.debug("生成delete sql error! 参数columns必须有值"  );
            }
            throw new Exception("参数columns必须有值！");
        }
    }
	/**
	 * 
	 * 2016年9月10日 上午10:46:05
	 * @author:cavion
	 * @描述：将SQL查询出来的map对象转成实体对象
	 * @param map
	 * @param pojoClass
	 * @return
	 * @throws Exception
	 */
	public static Object coverMapToBean(Map map, Class pojoClass){
		Object result=null;
		try {
			result = pojoClass.newInstance();
			List<FieldInfo> list = loadPojoSqlInfo(pojoClass);
			for (FieldInfo fieldInfo : list) {
				//String dbName = fieldInfo.getDbFieldName().toUpperCase();
				String fieldName = fieldInfo.getPojoFieldName();
				String setMethoName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				if (map.get(fieldName) != null) {
					Method m = pojoClass.getMethod(setMethoName, fieldInfo.getType());
					m.invoke(result, map.get(fieldName));
				}
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
	/**
	 * 
	 * 2016年9月10日 上午10:44:30
	 * @author:cavion
	 * @描述：根据用户指定的更新条件(字段)来生成delete的SQL
	 * @param pojoClass
	 * @param fieldInfoList
	 * @param columns
	 * @return
	 */
	private static String buildDeleteSqlByColumns(Class pojoClass, List<FieldInfo> fieldInfoList, String[] columns) {
		StringBuffer resultSql = new StringBuffer();
		if (deleteSqlCache.get(pojoClass.getName() + "_columns_"+columns.toString()) != null) {
			resultSql.append((String) deleteSqlCache.get(pojoClass.getName() + "_columns"));
		} else {
			resultSql.append(appendBaseDeleteSQL(pojoClass));
		}

		for (int i=0;i<columns.length;i++) {
			String column =columns[i];
			for (FieldInfo fieldInfo : fieldInfoList) {
				if (column.equals(fieldInfo.getPojoFieldName())) {
					resultSql.append(fieldInfo.getDbFieldName());
					resultSql.append("=:").append(column);
					break;
				}
			}
			if(i!=(columns.length-1)){
				resultSql.append(" and ");
			}
		}
		//resultSql.delete(resultSql.length() - 4, resultSql.length());
		return resultSql.toString();
	}
	/**
	 * @author：cavion
	 * @方法描述：根据map参数删除数据
	 * @param pojoClass
	 * @param params
	 * @return
	 * @throws Exception 
	 */
	public static String buildDeleteSqlByMapParams(Class pojoClass, Set<String> paramsKeyList) throws Exception {
		if(!CollectionUtils.isEmpty(paramsKeyList)){
            List<FieldInfo> fieldInfoList = loadPojoSqlInfo(pojoClass);
            StringBuffer resultSql = new StringBuffer();
    		String sqlcacheKeyStr = pojoClass.getName() + "_mapParams_"+paramsKeyList.toString();
    		if (deleteSqlCache.get(sqlcacheKeyStr) != null) {
    			resultSql.append((String) deleteSqlCache.get(sqlcacheKeyStr));
    		} else {
    			resultSql.append(appendBaseDeleteSQL(pojoClass));
    			for (String keystr : paramsKeyList) {
    				for (FieldInfo fieldInfo : fieldInfoList) {
						if(keystr.equals(fieldInfo.getPojoFieldName())) {
							resultSql.append(fieldInfo.getDbFieldName());
							resultSql.append("=:").append(keystr);
							break;
						}
					}
    			}
    		}
    		
            if(log.isDebugEnabled()){
                log.debug("delete sql is:"+resultSql.toString());
            }
            return resultSql.toString();
        }else{
            if(log.isDebugEnabled()){
                log.debug("生成delete sql error! 参数params必须有值"  );
            }
            throw new Exception("参数params必须有值！");
        }
		
	}
    /**
     * 
     * 2016年9月10日 上午10:23:07
     * @author:cavion
     * @描述：拼接select sql
     * @param pojoClass
     * @param fieldInfoList
     * @return
     */
	private static String buildSelectSql(Class pojoClass,List<FieldInfo> fieldInfoList){
        if(selectSqlCache.get(pojoClass.getName()) != null){
            return (String)selectSqlCache.get(pojoClass.getName());
        }
        StringBuffer sb=new StringBuffer();
        sb.append("select ");
        for(int i=0;i<fieldInfoList.size();i++){
        	FieldInfo fieldInfo=fieldInfoList.get(i);
        	if(!fieldInfo.getIsSelect()){//不查询的掠过
        		continue;
        	}
        	if(i!=0){
        		sb.append(",");
        	}
        	sb.append(" "+fieldInfo.getDbFieldName()+" as "+fieldInfo.getPojoFieldName()+" ");
        }
        sb.append(" from "+loadTableName(pojoClass));
        return sb.toString();
    }

	public static String buildSelectCountSql(Class pojoClass) {
		if (selectSqlCache.get(pojoClass.getName()+"_count") != null) {
			return (String) selectSqlCache.get(pojoClass.getName()+"_count");
		}
		StringBuffer sb = new StringBuffer();
		sb.append("select COUNT(1) AS countNum");
		sb.append(" from " + loadTableName(pojoClass));
		return sb.toString();
	}
    /**
     * 
     * 2016年9月10日 上午10:25:46
     * @author:cavion
     * @描述：通过类获取表名
     * @param pojoClass
     * @return
     */
	public static String loadTableName(Class pojoClass){
        if(pojoClass.isAnnotationPresent(TableMeta.class)){
        	TableMeta table = (TableMeta)pojoClass.getAnnotation(TableMeta.class);
            return table.catalog()+ "." + table.name();
        }else{
            return lowerStrToUnderline(pojoClass.getSimpleName());
        }
    }
    
    /**
     * 
     * 2016年9月10日 上午10:34:06
     * @author:cavion
     * @描述：拼接insert的SQL
     * @param pojoClass
     * @param fieldInfoList
     * @return
     */
	private static String buildInsertSql(Class pojoClass,List<FieldInfo> fieldInfoList){
        String result = null;
        if(insertSqlCache.get(pojoClass.getName()) != null){
            result = (String)insertSqlCache.get(pojoClass.getName());
            return result;
        }
        
        String tableName = loadTableName(pojoClass);
        
        StringBuffer temp1 = new StringBuffer();
        StringBuffer temp2 = new StringBuffer();
        for(FieldInfo fieldInfo : fieldInfoList){
            if(fieldInfo.getIsInsert()){
                temp1.append(fieldInfo.getDbFieldName()).append(",");
                temp2.append(":").append(fieldInfo.getPojoFieldName()).append(",");
            }
        }
        temp1.deleteCharAt(temp1.length()-1);
        temp2.deleteCharAt(temp2.length()-1);
        
        StringBuffer resultSql = new StringBuffer();
        resultSql.append("insert into ");
        resultSql.append(tableName);
        resultSql.append("(");
        resultSql.append(temp1);
        resultSql.append(") values (");
        resultSql.append(temp2);
        resultSql.append(")");
        
        result = resultSql.toString();
        insertSqlCache.put(pojoClass.getName(), result);
        return result;
    }
	/**
	 * 
	 * 2016年9月10日 上午10:33:43
	 * @author:cavion
	 * @描述：拼接根据主键来update的SQL
	 * @param pojoClass
	 * @param fieldInfoList
	 * @return
	 */
	private static String buildUpdateSqlByPK(Object objBean, List<FieldInfo> fieldInfoList) {
		Class pojoClass=objBean.getClass();
		String result = null;
		if (updateSqlCache.get(pojoClass.getName() + "_pk") != null) {
			result = (String) updateSqlCache.get(pojoClass.getName() + "_pk");
			return result;
		}

		StringBuffer resultSql = new StringBuffer();
		resultSql.append(appendBaseUpdateSQL(objBean, fieldInfoList));

		for (FieldInfo fieldInfo : fieldInfoList) {
			if (fieldInfo.getIsPk()) {
				resultSql.append(fieldInfo.getDbFieldName());
				resultSql.append("=:").append(fieldInfo.getPojoFieldName()).append(" and ");
			}
		}
		resultSql.delete(resultSql.length() - 4, resultSql.length());
		result = resultSql.toString();
		updateSqlCache.put(pojoClass.getName() + "_pk", result);

		return result;
	}
	/**
	 * @author：cavion
	 * @方法描述：根据唯一索引删除
	 * @param objBean
	 * @param fieldInfoList
	 * @return
	 */
	private static String buildUpdateSqlByUnique(Object objBean, List<FieldInfo> fieldInfoList) {
		Class pojoClass=objBean.getClass();
		String result = null;
		if (updateSqlCache.get(pojoClass.getName() + "_pk") != null) {
			result = (String) updateSqlCache.get(pojoClass.getName() + "_pk");
			return result;
		}

		StringBuffer resultSql = new StringBuffer();
		resultSql.append(appendBaseUpdateSQL(objBean, fieldInfoList));

		for (int i = 0; i<fieldInfoList.size();i++) {
			FieldInfo fieldInfo = fieldInfoList.get(i);
			if (fieldInfo.getIsUnique()) {
				resultSql.append(fieldInfo.getDbFieldName());
				resultSql.append("=:").append(fieldInfo.getPojoFieldName());
			}
			if(i != (fieldInfoList.size()-1)) {
				resultSql.append(" and ");
			}
		}
		result = resultSql.toString();
		updateSqlCache.put(pojoClass.getName() + "_pk", result);

		return result;
	}
	/**
	 * 
	 * 2016年9月10日 上午10:38:11
	 * @author:cavion
	 * @描述：根据用户指定的更新条件(字段)来生成update的SQL
	 * @param pojoClass
	 * @param fieldInfoList
	 * @param columns
	 * @return
	 */
	private static String buildUpdateSqlByColumns(Class pojoClass, List<FieldInfo> fieldInfoList, String[] columns) {
		StringBuffer resultSql = new StringBuffer();
		if (updateSqlCache.get(pojoClass.getName() + "_columns") != null) {
			resultSql.append((String) updateSqlCache.get(pojoClass.getName() + "_columns"));
		} else {
			resultSql.append(appendBaseDeleteSQL(pojoClass));
		}

		for (String column : columns) {
			for (FieldInfo fieldInfo : fieldInfoList) {
				if (column.equals(fieldInfo.getPojoFieldName())) {
					resultSql.append(fieldInfo.getDbFieldName());
					resultSql.append("=:").append(column).append(" and ");
					break;
				}
			}
		}
		resultSql.delete(resultSql.length() - 4, resultSql.length());
		return resultSql.toString();
	}
	/**
	 * 
	 * 2016年9月10日 上午10:35:25
	 * @author:cavion
	 * @描述：拼接update语句的where之前的sql
	 * @param pojoClass
	 * @param fieldInfoList
	 * @return
	 */
	private static String appendBaseUpdateSQL(Object objBean, List<FieldInfo> fieldInfoList){
		Class pojoClass=objBean.getClass();
        String result = null;
        if(updateSqlCache.get(pojoClass.getName()+"_columns") != null){
            result = (String)updateSqlCache.get(pojoClass.getName()+"_columns");
        }else{
            StringBuffer resultSql = new StringBuffer();
            String tableName = loadTableName(pojoClass);
            
            resultSql.append("update ").append(tableName).append(" set ");
            for(FieldInfo fieldInfo : fieldInfoList){
            	if(fieldInfo.getIsPk() || fieldInfo.getIsUnique()) {
            		continue;
            	}
            	Object fieldObjs=null;
            	try {
					Field field=pojoClass.getDeclaredField(fieldInfo.getPojoFieldName());
					field.setAccessible(true);
					fieldObjs=field.get(objBean);
				} catch (NoSuchFieldException e) {
					//当前类没有找到，找父类
					Field field;
					try {
						field = pojoClass.getSuperclass().getDeclaredField(fieldInfo.getPojoFieldName());
						field.setAccessible(true);
						fieldObjs=field.get(objBean);
					} catch (NoSuchFieldException e1) {
						try {
							field = pojoClass.getSuperclass().getDeclaredField(fieldInfo.getPojoFieldName());
							field.setAccessible(true);
							fieldObjs=field.get(objBean);
						} catch (NoSuchFieldException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						} catch (SecurityException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						} catch (IllegalArgumentException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						} catch (IllegalAccessException e2) {
							// TODO Auto-generated catch block
							e2.printStackTrace();
						}
					} catch (SecurityException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IllegalArgumentException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IllegalAccessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                if(fieldInfo.getIsUpdate() && !fieldInfo.getIsPk() && !RkObjectUtil.isEmpty(fieldObjs) && !fieldInfo.getIsUnique()){
                    resultSql.append(fieldInfo.getDbFieldName());
                    resultSql.append("=:").append(fieldInfo.getPojoFieldName()).append(",");
                }
            }
            resultSql.deleteCharAt(resultSql.length()-1);
            resultSql.append(" where ");
            
            result = resultSql.toString();
            updateSqlCache.put(pojoClass.getName()+"_columns", result);
        }
        return result;
    }
	/**
	 * 
	 * 2016年9月10日 上午10:40:40
	 * @author:cavion
	 * @描述：生成根据主键生成删除的SQL
	 * @param pojoClass
	 * @param fieldInfoList
	 * @return
	 */
	private static String buildDeleteSqlByPK(Class pojoClass,List<FieldInfo> fieldInfoList){
        String result = null;
        if(deleteSqlCache.get(pojoClass.getName()+"_pk") != null){
            result = (String)deleteSqlCache.get(pojoClass.getName()+"_pk");
            return result;
        }
        
        StringBuffer resultSql = new StringBuffer();
        resultSql.append(appendBaseDeleteSQL(pojoClass));
        
        for(int i=0; i<fieldInfoList.size(); i++){
        	FieldInfo fieldInfo = fieldInfoList.get(i);
            if(fieldInfo.getIsPk()){
                resultSql.append(fieldInfo.getDbFieldName());
                resultSql.append("=:").append(fieldInfo.getPojoFieldName());
            }
            if(i != (fieldInfoList.size()-1)) {
            	resultSql.append(" or ");
            }
        }
        result = resultSql.toString();
        deleteSqlCache.put(pojoClass.getName()+"_pk", result);
        
        return result;
    }
	/**
	 * @author：cavion
	 * @方法描述：根据唯一索引删除
	 * @param pojoClass
	 * @param fieldInfoList
	 * @return
	 */
	private static String buildDeleteSqlByUnique(Class pojoClass,List<FieldInfo> fieldInfoList){
        String result = null;
        if(deleteSqlCache.get(pojoClass.getName()+"_unique") != null){
            result = (String)deleteSqlCache.get(pojoClass.getName()+"Unique");
            return result;
        }
        
        StringBuffer resultSql = new StringBuffer();
        resultSql.append(appendBaseDeleteSQL(pojoClass));
        
        for(int i=0; i<fieldInfoList.size(); i++){
        	FieldInfo fieldInfo = fieldInfoList.get(i);
            if(fieldInfo.getIsUnique()){
                resultSql.append(fieldInfo.getDbFieldName());
                resultSql.append("=:").append(fieldInfo.getPojoFieldName());
            }
            if(i != (fieldInfoList.size()-1)) {
            	resultSql.append(" or ");
            }
        }
        result = resultSql.toString();
        deleteSqlCache.put(pojoClass.getName()+"Unique", result);
        
        return result;
    }
	/**
	 * 
	 * 2016年9月10日 上午10:42:28
	 * @author:cavion
	 * @描述： 拼接delete语句的where之前的sql
	 * @param pojoClass
	 * @return
	 */
	private static String appendBaseDeleteSQL(Class pojoClass) {
		if (deleteSqlCache.get(pojoClass.getName() + "_columns") != null) {
			return (String) deleteSqlCache.get(pojoClass.getName() + "_columns");
		} else {
			String result = "delete from " + loadTableName(pojoClass) + " where ";
			deleteSqlCache.put(pojoClass.getName() + "_columns", result);
			return result;
		}
	}
	/**
	 * 
	 * 2016年9月10日 上午10:36:14
	 * @author:cavion
	 * @描述：加载读取pojo的注解信息
	 * @param pojoClass
	 * @return
	 */
	public static List<FieldInfo> loadPojoSqlInfo(Class pojoClass){
        List<FieldInfo> resultList = null;
        if(null == cacheMap.get(pojoClass.getName())){
        	resultList=new ArrayList<FieldInfo>();
            resultList = getClassField(pojoClass,resultList);
            cacheMap.put(pojoClass.getName(), resultList);
        }else{
            resultList = (List<FieldInfo>)cacheMap.get(pojoClass.getName());
        }
        
        return resultList;
    }

	private static List<FieldInfo> getClassField(Class pojoClass,List<FieldInfo> resultList) {
		Field[] fields = pojoClass.getDeclaredFields();
		for (Field field : fields) {
			if ("serialVersionUID".equals(field.getName())) {
				continue;
			}
			FieldInfo fieldInfo = new FieldInfo();
			fieldInfo.setPojoFieldName(field.getName());
			if (field.isAnnotationPresent(FieldMeta.class)) {
				FieldMeta fieldMeta = field.getAnnotation(FieldMeta.class);
				String name = fieldMeta.name();// 得到配置的数据库字段名
				boolean isPK = fieldMeta.isPK();// 是否主键
				boolean isInsert = fieldMeta.isInsert();
				boolean isUpdate = fieldMeta.isUpdate();
				boolean isSelect=fieldMeta.isSelect();//是否查询

				if (RkStrUtil.isEmpty(name)) {// 没有设置数据库的字段名，则取pojo的字段名
					fieldInfo.setDbFieldName(lowerStrToUnderline(field.getName()));
				} else {
					fieldInfo.setDbFieldName(name);
				}
				if (isPK) {
					fieldInfo.setIsPk(true);
				}
				if (!isInsert) {
					fieldInfo.setIsInsert(false);
				}
				if (!isUpdate) {
					fieldInfo.setIsUpdate(false);
				}
				if(!isSelect){
					fieldInfo.setIsSelect(false);
				}
			}
			fieldInfo.setType(field.getType());
			resultList.add(fieldInfo);
		}
		//递归获取父类
		 Class pPojoClass=pojoClass.getSuperclass();//获取父类
         if(pPojoClass!=null){
        	 getClassField(pPojoClass,resultList);
         }
         return resultList;
	}
    /**
     * 
     * 2016年9月10日 上午10:09:37
     * @author:cavion
     * @描述：将大写字母转换成下划线加小写字母,例:userName--> user_name
     * @param str
     * @return
     */
    private static String lowerStrToUnderline(String str) {
        if(RkStrUtil.isEmpty(str)){
            return "";
        }
        StringBuilder sb = new StringBuilder(str);
        char c;
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                sb.replace(i+count, i+count+1, (c+"").toLowerCase());
                sb.insert(i+count, "_");
                count++;
            }
        }
        return sb.toString();
    }
}
